package com.vehicle.admin.web.Redis;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * Created by yantingbin on 2017/7/26.
 */
public class Ticketing {
    private static boolean isDone = false;
    private static long j = 0;

    public static void main(String[] args) {

        Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
//...生成上传凭证，然后准备上传
        String accessKey = "your access key";
        String secretKey = "your secret key";
        String bucket = "your bucket name";
//如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "/home/qiniu/test.mp4";
//默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
        try {
            //设置断点续传文件进度保存目录
            FileRecorder fileRecorder = new FileRecorder(localTempDir);
            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
            try {
                Response response = uploadManager.put(localFilePath, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        Thread thread1 = new Thread(() -> {
            System.out.println(System.currentTimeMillis());
            long i = 0;
            long s = 0;
            while (!(j > 0)) {
                //i++;
                //System.out.println("i="+i);
/*                synchronized (Ticketing.class){
                    s=i+1;
                }*/
                synchronized (Ticketing.class) {
                    s = j;
                }
                //System.out.println(s);
            }
            System.out.println(System.currentTimeMillis());
        });

        thread1.start();
        try {
            Thread.currentThread().sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isDone = true;
        j = 10;
    }
}
